﻿using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace pandx.Mulan.Flows
{
    public class FlowLine
    {
        public string From { get; set; }
        public string To { get; set; }
        public string Label { get; set; }
        public List<Condition> Conditions { get; set; }

        public bool Compare(JObject formData)
        {
            bool result = true;
            foreach (var condition in Conditions)
            {
                bool isDigital = decimal.TryParse(condition.Value, out decimal conditionValue);
                var fieldValue = formData.GetValue(condition.Field.ToLower()).ToString();
                if (isDigital)
                {
                    decimal.TryParse(fieldValue,out decimal formValue);
                    switch (condition.Operation)
                    {
                        case Condition.Equal:
                            result &= formValue == conditionValue;
                            break;
                        case Condition.Greater:
                            result &= formValue > conditionValue;
                            break;
                        case Condition.GreaterEqual:
                            result &= formValue >= conditionValue;
                            break;
                        case Condition.Less:
                            result &= formValue < conditionValue;
                            break;
                        case Condition.LessEqual:
                            result &= formValue <= conditionValue;
                            break;
                    }
                }
                else
                {
                    result &= condition.Value.ToLower() == fieldValue;
                }
            }

            return result;
        }
    }

    public class Condition
    {
        public const string Greater = ">";
        public const string GreaterEqual = ">=";
        public const string Less = "<";
        public const string LessEqual = "<=";
        public const string Equal = "=";
        
        public string Field { get; set; }
        public string Operation { get; set; }
        public string Value { get; set; }
    }
}
